package com.sky.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sky.constant.MessageConstant;
import com.sky.constant.StatusConstant;
import com.sky.dto.CategoryPageQueryDTO;
import com.sky.entity.Category;
import com.sky.entity.Dish;
import com.sky.entity.Setmeal;
import com.sky.exception.DeletionNotAllowedException;
import com.sky.mapper.CategoryMapper;
import com.sky.mapper.DishMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.result.PageResult;
import com.sky.service.CategoryService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class CategoryServiceImpl implements CategoryService {

    @Autowired
    private CategoryMapper categoryMapper;

    @Autowired
    private DishMapper dishMapper;

    @Autowired
    private SetmealMapper setmealMapper;

    @Override
    public PageResult page(CategoryPageQueryDTO categoryPageQueryDTO) {
        PageHelper.startPage(categoryPageQueryDTO.getPage(), categoryPageQueryDTO.getPageSize());
        Category category = new Category();
        BeanUtils.copyProperties(categoryPageQueryDTO, category);
        List<Category> categories = categoryMapper.queryByCondition(category);
        Page<Category> page = (Page) categories;
        return new PageResult(page.getTotal(), page.getResult());
    }

    @Override
    public void add(Category category) {
        category.setStatus(StatusConstant.ENABLE);
        categoryMapper.insert(category);
    }

    @Override
    public void update(Category category) {
        category.setStatus(StatusConstant.ENABLE);
        categoryMapper.update(category);
    }

    @Override
    public void enableOrDisable(Long id, Integer status) {
        Category category = new Category();
        category.setStatus(status);
        category.setId(id);
        categoryMapper.update(category);
    }

    @Override
    public void delete(Long id) {
        //启用的分类不允许删除
        Category category = categoryMapper.getById(id);
        if (category.getStatus() == StatusConstant.ENABLE) {
            throw new DeletionNotAllowedException(MessageConstant.CATEGORY_ON_SALE);
        }
        //判断分类是否被菜品关联
        List<Dish> dishes = dishMapper.queryByCategoryId(id);
        if (!dishes.isEmpty()) {
            throw new DeletionNotAllowedException(MessageConstant.CATEGORY_BE_RELATED_BY_DISH);
        }
        List<Setmeal> setmeals = setmealMapper.queryByCategoryId(id);
        if (!dishes.isEmpty()) {
            throw new DeletionNotAllowedException(MessageConstant.CATEGORY_BE_RELATED_BY_SETMEAL);
        }
        categoryMapper.delete(id);
    }

    @Override
    public List<Category> getByType(Integer type) {
        Category category = new Category();
        category.setType(type);
        category.setStatus(StatusConstant.ENABLE);
        return categoryMapper.queryByCondition(category);
    }

    @Override
    public List<Category> list() {
        Category category = new Category();
        return categoryMapper.queryByCondition(category);
    }
}
